from fastapi import APIRouter, Request,Query
from fastapi.responses import StreamingResponse
from model.db import Session
from pydantic import BaseModel,Field
from exception import JsonException
from enum import Enum
from model.user import User
from controller.rtn import Rtn,RtnResp
from fastapi.encoders import jsonable_encoder
from model.test import Test
from model.config import Config

router = APIRouter()


class Rtt(BaseModel):
    a:str

@router.get("/test",response_model=Rtt)
async def test(request: Request):

    return {
        "a":"bb",
        "a2":"bb"
    }


@router.get("/test2", summary="获取消息记录列表")
async def test2(
        request: Request,
        page: int = Query(1, ge=1, le=10000), 
        limit: int = Query(10, ge=1, le=10000),
):
    
    user:User = request.state.userInfo

    offset = limit * (page - 1)

    with Session() as session:
        # 根据 chat_history_id 查询聊天记录
        query = session.query(Test)

        # 执行查询并分页
        list = query.order_by(Test.id.desc()).offset(offset).limit(limit).all()
        # 获取总数
        count = query.count()

    
    return Rtn({
        "list": list,
        "count":count
    })
